<?php
class user_productPriceTCEFormHook
{
	function getMainFields_preProcess($table,&$row,$pObj)
	{
		global $BE_USER;
		if ($table == 'tx_commerce_products' && is_numeric($row['uid']) && !$BE_USER->isAdmin())
		{
			//debug($BE_USER);
			
			$row['user_farm_commerce_price_and_rest'] = '';
				//Получаем список всех филиалов в которых текущий пользователь может редактировать
				//информацию о ценах и остатках товаров
			
			$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
				'uid',
				'user_farm_branches',
				'(leader_id = '.$BE_USER->user['uid'].' OR group_id IN ('.$BE_USER->groupList.')) '.t3lib_BEfunc::deleteClause('user_farm_branches')
			);
			
			
			if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) > 0)
			{
				$allowedBranches = array();
				while($branch = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
				{
					$allowedBranches[] = $branch['uid'];
				}
				
				$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
					'uid',
					'user_farm_commerce_product_branch_mm',
					'product_id = '.$row['uid'].' AND branch_id IN ('.implode(',', $allowedBranches).') '.t3lib_BEfunc::deleteClause('user_farm_commerce_product_branch_mm')
				);
				
				if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) > 0)
				{
					$allowedPriceAndRestRecords = array();
					while($priceAndRestRecord = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
					{
						$allowedPriceAndRestRecords[] = $priceAndRestRecord['uid'];
					}
					
					$row['user_farm_commerce_price_and_rest'] = implode(',', $allowedPriceAndRestRecords);
				}
			}
		}
	}
}
?>